/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.sanerzone.smscenter.modules.sms.web;

import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.sanerzone.jeebase.common.config.Global;
import com.sanerzone.jeebase.common.persistence.Page;
import com.sanerzone.jeebase.common.utils.StringUtils;
import com.sanerzone.jeebase.common.web.BaseController;
import com.sanerzone.jeebase.modules.sys.entity.Dict;
import com.sanerzone.jeebase.modules.sys.utils.DictUtils;
import com.sanerzone.smscenter.modules.sms.entity.SmsCompany;
import com.sanerzone.smscenter.modules.sms.service.SmsCompanyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * aaaaController
 * @author zequs
 * @version 2017-08-03
 */
@Controller
@RequestMapping(value = "${adminPath}/sms/smsCompany")
public class SmsCompanyController extends BaseController {

	@Autowired
	private SmsCompanyService smsCompanyService;
	
	@ModelAttribute
	public SmsCompany get(@RequestParam(required=false) String id) {
		SmsCompany entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = smsCompanyService.get(id);
		}
		if (entity == null){
			entity = new SmsCompany();
		}
		return entity;
	}
	
	@RequiresPermissions("sms:smsCompany:view")
	@RequestMapping(value = {"list", ""})
	public String list(SmsCompany smsCompany, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SmsCompany> page = smsCompanyService.findPage(new Page<SmsCompany>(request, response), smsCompany);
		model.addAttribute("page", page);
		return "modules/sms/smsCompanyList";
	}

	@RequiresPermissions("sms:smsCompany:view")
	@RequestMapping(value = "form")
	public String form(SmsCompany smsCompany, Model model) {
		model.addAttribute("smsCompany", smsCompany);
		String type = smsCompany.getType();
		List<String> typeList = Lists.newArrayList();
		List<Dict> types = Lists.newArrayList();
		List<Dict> dicts = DictUtils.getDictList("company_type");
		if (!Strings.isNullOrEmpty(type)) {
			if (type.contains(",")) {
				typeList = Lists.newArrayList(Splitter.on(',') 
		       			.trimResults() 
		       			.omitEmptyStrings() 
		       			.split(type));
				
			} else {
				typeList.add(type);
			}
			if (typeList != null && typeList.size() > 0) {
				for (String value : typeList) {
					for (Dict dict : dicts) {
						if (value.equals(dict.getValue())) {
							types.add(dict);
						}
					}
				}
			}
		}
		smsCompany.setTypeList(types);
		model.addAttribute("types", DictUtils.getDictList("company_type"));
		model.addAttribute("smsCompany", smsCompany);
		return "modules/sms/smsCompanyForm";
	}

	@RequiresPermissions("sms:smsCompany:edit")
	@RequestMapping(value = "save")
	public String save(SmsCompany smsCompany, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, smsCompany)){
			return form(smsCompany, model);
		}
		if (smsCompany.getTypeList() != null && smsCompany.getTypeList().size() > 0) {
    		String types = "";
    		for (Dict dict : smsCompany.getTypeList()) {
    			types = types + "," + dict.getId();
    		}
    		smsCompany.setType(types.substring(1));
		}
		smsCompanyService.save(smsCompany);
		addMessage(redirectAttributes, "保存公司生成成功");
		return "redirect:"+ Global.getAdminPath()+"/sms/smsCompany/?repage";
	}
	
	@RequiresPermissions("sms:smsCompany:edit")
	@RequestMapping(value = "delete")
	public String delete(SmsCompany smsCompany, RedirectAttributes redirectAttributes) {
		smsCompanyService.delete(smsCompany);
		addMessage(redirectAttributes, "删除公司生成成功");
		return "redirect:"+Global.getAdminPath()+"/sms/smsCompany/?repage";
	}
}